package pro.seac.twitter.web.view;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import pro.seac.twitter.web.Navigateable;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@Component
@Scope("prototype")
@Navigateable("login")
public class LoginView extends BaseView {
	private static final long serialVersionUID = 1L;
	private VerticalLayout formContainer;

	private TextField accountNameField;
	private PasswordField passwordField;

	@Autowired
	private AuthenticationManager authenticationManager;

	@Override
	protected com.vaadin.ui.Component initContent() {
		formContainer = (VerticalLayout) super.initContent();

		formContainer.addComponent(accountNameField = new TextField("Nazwa konta"));
		accountNameField.setNullRepresentation(StringUtils.EMPTY);
		accountNameField.setRequired(true);

		formContainer.addComponent(passwordField = new PasswordField("Haslo"));
		passwordField.setNullRepresentation(StringUtils.EMPTY);
		passwordField.setRequired(true);

		HorizontalLayout actions = new HorizontalLayout();
		actions.setSpacing(true);
		Button actionButton;
		actions.addComponent(actionButton = new Button("zaloguj"));
		actionButton.addClickListener(new ClickListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				login();
			}
		});
		actions.addComponent(actionButton = new Button("anuluj"));
		actionButton.addClickListener(new ClickListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				goToPublicHomeView();
			}
		});
		formContainer.addComponent(actions);
		return formContainer;
	}

	public void login() {
		if ("".equals(accountNameField.getValue())) {
			Notification.show("B\u0142\u0105d", "Uzupe\u0142nij poprawnie wszystkie pola", Type.ERROR_MESSAGE);
		} else {
			try {
				UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(accountNameField.getValue(), passwordField.getValue());
				Authentication authentication = authenticationManager.authenticate(token);
				SecurityContextHolder.clearContext();
				SecurityContextHolder.getContext().setAuthentication(authentication);
				goToUserHomeView();
			} catch (AuthenticationException ex) {
				Notification.show("B\u0142\u0105d", "B\u0142\u0119dnie podana nazwa u\u017Cytkownika i has\u0142o", Type.ERROR_MESSAGE);
				accountNameField.setValue("");
				passwordField.setValue("");
			}
		}
	}

	@Override
	protected String getTitle() {
		return "Zaloguj si\u0119";
	}
}
